{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "043ba3cd-6a8b-415c-9574-de033df2862d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:08:03.753561Z",
     "iopub.status.busy": "2022-02-07T09:08:03.753267Z",
     "iopub.status.idle": "2022-02-07T09:08:04.060586Z",
     "shell.execute_reply": "2022-02-07T09:08:04.059811Z",
     "shell.execute_reply.started": "2022-02-07T09:08:03.753491Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d08deb9-c29e-4daa-a132-e06fdbdf10c5",
   "metadata": {},
   "source": [
    "1. vstack-垂直方向叠加(按行叠加)，数组列数必须一致\n",
    "2. hstack-水平方向叠加(按列叠加)，数组行数必须一致\n",
    "3. concatenate手动指定axis参数来设置具体的叠加方向\n",
    "    1. axis=0，垂直方向，即按行叠加\n",
    "    2. axis=1，水平方向，即按列叠加\n",
    "    3. axis=None叠加后转扁平化成1维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "abcf1272-0e94-4d4e-8209-f4a6ee82bac5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:12:04.445853Z",
     "iopub.status.busy": "2022-02-07T09:12:04.445606Z",
     "iopub.status.idle": "2022-02-07T09:12:04.455125Z",
     "shell.execute_reply": "2022-02-07T09:12:04.454045Z",
     "shell.execute_reply.started": "2022-02-07T09:12:04.445826Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[7 5 0 5 6]\n",
      " [8 3 7 3 2]\n",
      " [9 8 3 4 7]]\n",
      "====================\n",
      "a2:\n",
      "[[3 7 7 4 5]\n",
      " [5 0 9 0 6]]\n",
      "====================\n",
      "a3=np.vstack([a1,a2]):\n",
      "[[7 5 0 5 6]\n",
      " [8 3 7 3 2]\n",
      " [9 8 3 4 7]\n",
      " [3 7 7 4 5]\n",
      " [5 0 9 0 6]]\n"
     ]
    }
   ],
   "source": [
    "# 垂直方向的叠加vstack(按行叠加)\n",
    "a1 = np.random.randint(0, 10, size=(3, 5))  # 创建3行5列的数组a1，其中元素为0到9之间的随机整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "a2 = np.random.randint(0, 10, size=(2, 5))  # 创建2行5列的数组a2，其中元素为0到9之间的随机整数\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "a3 = np.vstack([a1, a2])  # 把数组a2垂直叠加到数组a1下面\n",
    "print(\"a3=np.vstack([a1,a2]):\\n{}\".format(a3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3259e24b-1bc2-46d4-9e52-f0f50afed816",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:16:13.147436Z",
     "iopub.status.busy": "2022-02-07T09:16:13.147169Z",
     "iopub.status.idle": "2022-02-07T09:16:13.154410Z",
     "shell.execute_reply": "2022-02-07T09:16:13.153383Z",
     "shell.execute_reply.started": "2022-02-07T09:16:13.147410Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a4:\n",
      "[[6 5 2 4 6]\n",
      " [4 8 5 7 7]\n",
      " [5 8 7 1 0]]\n",
      "====================\n",
      "a5:\n",
      "[[4]\n",
      " [9]\n",
      " [1]]\n",
      "====================\n",
      "a6=np.hstack([a5,a4]):\n",
      "[[4 6 5 2 4 6]\n",
      " [9 4 8 5 7 7]\n",
      " [1 5 8 7 1 0]]\n"
     ]
    }
   ],
   "source": [
    "# 水平方向的叠加hstack(按列叠加)\n",
    "a4 = np.random.randint(0, 10, size=(3, 5))  # 创建3行5列的数组a4，其中元素为0到9之间的随机整数\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "a5 = np.random.randint(0, 10, size=(3, 1))  # 创建3行1列的数组a5，其中元素为0到9之间的随机整数\n",
    "print(\"a5:\\n{}\".format(a5))\n",
    "print(\"=\" * 20)\n",
    "a6 = np.hstack([a5, a4])  # 把数组a5叠加到a4前面\n",
    "print(\"a6=np.hstack([a5,a4]):\\n{}\".format(a6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1066484c-c914-4d7a-bed4-962efcdc7fc9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:23:17.609262Z",
     "iopub.status.busy": "2022-02-07T09:23:17.609001Z",
     "iopub.status.idle": "2022-02-07T09:23:17.615035Z",
     "shell.execute_reply": "2022-02-07T09:23:17.613980Z",
     "shell.execute_reply.started": "2022-02-07T09:23:17.609235Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[7 5 0 5 6]\n",
      " [8 3 7 3 2]\n",
      " [9 8 3 4 7]]\n",
      "====================\n",
      "a2:\n",
      "[[3 7 7 4 5]\n",
      " [5 0 9 0 6]]\n",
      "====================\n",
      "a7 = np.concatenate([a1,a2], axis=0):\n",
      "[[7 5 0 5 6]\n",
      " [8 3 7 3 2]\n",
      " [9 8 3 4 7]\n",
      " [3 7 7 4 5]\n",
      " [5 0 9 0 6]]\n"
     ]
    }
   ],
   "source": [
    "# 手动指定叠加方向(垂直，即按行叠加)\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "a7 = np.concatenate([a1, a2], axis=0)  # 把数组a2垂直叠加到数组a1下面\n",
    "print(\"a7 = np.concatenate([a1,a2], axis=0):\\n{}\".format(a7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bbebd43a-0db5-4072-8279-b91934e949ef",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:23:21.487243Z",
     "iopub.status.busy": "2022-02-07T09:23:21.486984Z",
     "iopub.status.idle": "2022-02-07T09:23:21.492523Z",
     "shell.execute_reply": "2022-02-07T09:23:21.491884Z",
     "shell.execute_reply.started": "2022-02-07T09:23:21.487216Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a4:\n",
      "[[6 5 2 4 6]\n",
      " [4 8 5 7 7]\n",
      " [5 8 7 1 0]]\n",
      "====================\n",
      "a5:\n",
      "[[4]\n",
      " [9]\n",
      " [1]]\n",
      "====================\n",
      "a8=np.concatenate([a5,a4], axis=1):\n",
      "[[4 6 5 2 4 6]\n",
      " [9 4 8 5 7 7]\n",
      " [1 5 8 7 1 0]]\n"
     ]
    }
   ],
   "source": [
    "# 手动指定叠加方向(水平，即按列叠加)\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "print(\"a5:\\n{}\".format(a5))\n",
    "print(\"=\" * 20)\n",
    "a8 = np.concatenate([a5, a4], axis=1)  # 把数组a5叠加到a4前面\n",
    "print(\"a8=np.concatenate([a5,a4], axis=1):\\n{}\".format(a8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fe82936d-1fb8-4fe8-b089-86c97db42de3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-07T09:25:54.674889Z",
     "iopub.status.busy": "2022-02-07T09:25:54.674318Z",
     "iopub.status.idle": "2022-02-07T09:25:54.680933Z",
     "shell.execute_reply": "2022-02-07T09:25:54.679883Z",
     "shell.execute_reply.started": "2022-02-07T09:25:54.674860Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a4:\n",
      "[[6 5 2 4 6]\n",
      " [4 8 5 7 7]\n",
      " [5 8 7 1 0]]\n",
      "====================\n",
      "a5:\n",
      "[[4]\n",
      " [9]\n",
      " [1]]\n",
      "====================\n",
      "a9=np.concatenate([a5,a4], axis=None):\n",
      "[4 9 1 6 5 2 4 6 4 8 5 7 7 5 8 7 1 0]\n"
     ]
    }
   ],
   "source": [
    "# 手动指定叠加方向(扁平化)\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "print(\"a5:\\n{}\".format(a5))\n",
    "print(\"=\" * 20)\n",
    "a9 = np.concatenate([a5, a4], axis=None)  # 把数组a5叠加到a4前面\n",
    "print(\"a9=np.concatenate([a5,a4], axis=None):\\n{}\".format(a9))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
